<?php
class ml_model_atNick_rdsFollowList extends ml_model_redis  
{        
    const PFX_SET_ATNICK_FOLLOWLIST = 'atFIn:';
    const PFX_SET_ATNICK_LATESTAT = 'atFLA:';
    const PFX_SET_ATNICK_LATESTAT_FRIEND = 10;
    const PFX_SET_ATNICK_LATESTAT_FOLLOW = 20;
    
    function __construct() {
        if(!$this->init_rds('meila_cache'))
            return false;
    }
    
    public function setFollowList($uid, $aFollowUids){
        
        $key = self::PFX_SET_ATNICK_FOLLOWLIST.$uid;
        
        if (!is_array($aFollowUids)) {
            return $this->sAdd($key, intval($aFollowUids));
        }
        
        foreach ($aFollowUids as $value) {
            $re = $this->sAdd($key, intval($value));
            if (!$re) return false;
        }
    }
    
    public function removeFollowList($uid, $aFollowUids){
        
        $key = self::PFX_SET_ATNICK_FOLLOWLIST.$uid;
        
        if (!is_array($aFollowUids)) {
            return $this->sRem($key, intval($aFollowUids));
        }
        
        foreach ($aFollowUids as $value) {
            $re = $this->sRem($key, intval($value));
            if (!$re) return false;
        }

    }
    
    public function isMemberFollowList($uid, $fuid) {
        
        $key = self::PFX_SET_ATNICK_FOLLOWLIST.$uid;
        
        return $this->sIsMember($key, $fuid);
    }
    
    
    public function getAllFollowList(){
        
        $pattern = self::PFX_SET_ATNICK_FOLLOWLIST.'*';
        
        return $this->keys($pattern);
    }
    
    public function getAllLatestList(){
        
        $pattern = self::PFX_SET_ATNICK_LATESTAT.'*';
        
        return $this->keys($pattern);
    }
    
    
    public function removeLatestList_byUid($uid, $atUid){
        
        $key = self::PFX_SET_ATNICK_LATESTAT.$uid;
        
        return $this->lRem($key, $atUid);
    }
    
    public function pushLatestAtUid($uid, $atUid){
        
        $key = self::PFX_SET_ATNICK_LATESTAT.$uid;
        
        return $this->lPush($key, $atUid);
    }
    
    public function trimLatestAt($uid, $number){
        
        $key = self::PFX_SET_ATNICK_LATESTAT.$uid;
        
        return $this->lTrim($key, 0, $number);
    }
    
    public function getLatestAt($uid, $start=0, $end=-1){
        
        $key = self::PFX_SET_ATNICK_LATESTAT.$uid;
        
        return $this->lRange($key, $start, $end);
    }
    
    public function getLatestAtLength($uid){
        
        $key = self::PFX_SET_ATNICK_LATESTAT.$uid;
        
        return $this->lSize($key);
    }
    
    public function insertLatestAt($uid, $atUid){
        
        return $this->pushLatestAtUid($uid, $atUid);
    }
    
    public function insertLatestFollow($uid, $fUid){
        
        $key = self::PFX_SET_ATNICK_LATESTAT.$uid;
        
        $re = $this->lInsert($key, self::PFX_SET_ATNICK_LATESTAT_FOLLOW, $fUid, 1);
        if ($re <= 0) {
            $this->rPush($key, self::PFX_SET_ATNICK_LATESTAT_FOLLOW);
            $this->rPush($key, $fUid);
        }
    }
    
    public function insertLatestFriend($uid, $fUid){
        
        $key = self::PFX_SET_ATNICK_LATESTAT.$uid;
        
        $re = $this->lInsert($key, self::PFX_SET_ATNICK_LATESTAT_FRIEND, $fUid, 1);
        
        if ($re <= 0) {
            $re = $this->lInsert($key, self::PFX_SET_ATNICK_LATESTAT_FOLLOW, self::PFX_SET_ATNICK_LATESTAT_FRIEND, -1);
            $re = $this->lInsert($key, self::PFX_SET_ATNICK_LATESTAT_FRIEND, $fUid, 1);
            if ($re <= 0) {
                
                $this->rPush($key, self::PFX_SET_ATNICK_LATESTAT_FRIEND);
                $re = $this->rPush($key, $fUid);
                
                if (!$re) {
                    return false;
                }
            }
        }
    }
    
    public function getLatest_byIndex($uid, $index) {
        
        $key = self::PFX_SET_ATNICK_LATESTAT.$uid;
        
        return $this->lGet($key, $index);
    }
}